Imports System.Collections.Generic
Imports System.Drawing
Imports System.Globalization
Imports System.Windows.Forms
Imports Vintasoft.Pdf
Imports Vintasoft.Pdf.Content.TextExtraction
Imports Vintasoft.Pdf.Tree.Fonts

Namespace Pdf
	Public Partial Class AddTextForm
		Inherits Form

		#Region "Fields"

		Private _fonts As List(Of PdfFont)
		Private _document As PdfDocument
		Private _tempDocument As PdfDocument = Nothing

		#End Region



		#Region "Constructor"

		Public Sub New(document As PdfDocument, fonts As IList(Of PdfFont), initalText As String)
			InitializeComponent()
			_document = document
			_fonts = New List(Of PdfFont)()
			_fonts.AddRange(fonts)
			If _fonts.Count > 0 Then
				For i As Integer = 0 To _fonts.Count - 1
					fontComboBox.Items.Add(String.Format("[№{1}] {0}", _fonts(i).FontName, _fonts(i).ObjectNumber))
				Next
				fontComboBox.SelectedIndex = 0
			End If
			fontSizeComboBox.Text = "12"
			textBox.Text = initalText
			buttonOk.Focus()
		End Sub

		#End Region



		#Region "Properties"

		Public ReadOnly Property TextToAdd() As String
			Get
				Return textBox.Text
			End Get
		End Property

		Public ReadOnly Property TextFont() As PdfFont
			Get
				Return _fonts(fontComboBox.SelectedIndex)
			End Get
		End Property

		Public ReadOnly Property TextSize() As Single
			Get
				Return Single.Parse(fontSizeComboBox.Text, CultureInfo.InvariantCulture)
			End Get
		End Property

		Public ReadOnly Property TextColor() As Color
			Get
				Return colorPanel.BackColor
			End Get
		End Property

		#End Region



		#Region "Methods"

		Private Sub buttonCancel_Click(sender As Object, e As EventArgs)
			If _tempDocument IsNot Nothing Then
				_tempDocument.Dispose()
			End If
			DialogResult = DialogResult.Cancel
		End Sub

		Private Sub buttonOk_Click(sender As Object, e As EventArgs)
			If fontComboBox.SelectedItem Is Nothing Then
				DemosTools.ShowWarningMessage("Text font is not specified.")
				Return
			End If
			If _tempDocument IsNot Nothing Then
				_tempDocument.Dispose()
			End If
			DialogResult = DialogResult.OK
		End Sub

		Private Sub textBox_TextChanged(sender As Object, e As EventArgs)
			buttonOk.Enabled = textBox.Text.Length > 0
		End Sub

		Private Sub setColorButton_Click(sender As Object, e As EventArgs)
			Dim colorDialog As New ColorDialog()
			colorDialog.Color = colorPanel.BackColor
			If colorDialog.ShowDialog() = DialogResult.OK Then
				colorPanel.BackColor = colorDialog.Color
			End If
		End Sub

		Private Sub fontComboBox_SelectedIndexChanged(sender As Object, e As EventArgs)
			pdfFontViewerControl.PdfFont = _fonts(fontComboBox.SelectedIndex)
		End Sub

		Private Sub pdfFontViewerControl_MouseDown(sender As Object, e As MouseEventArgs)
			Dim symbol As TextSymbol = pdfFontViewerControl.GetTextSymbol(e.Location)
			If symbol IsNot Nothing Then
				textBox.AppendText(symbol.Symbol.ToString())
			End If
		End Sub

		Private Sub addFontButton_Click(sender As Object, e As EventArgs)
			Dim form As New CreateFontForm(_document)
			If form.ShowDialog() = DialogResult.OK Then
				Dim font As PdfFont = form.NewFont
				_fonts.Add(font)
				fontComboBox.Items.Add(String.Format("[№{1}] {0}", font.FontName, font.ObjectNumber))
				fontComboBox.SelectedIndex = fontComboBox.Items.Count - 1
			End If
		End Sub

		#End Region


	End Class
End Namespace
